﻿using System;
using System.Net;
using System.Text;
using System.Web.UI;
using Baby.Growup.Common;
using Baby.GrowUp.Model;

namespace Baby.GrowUp.Web.Admin
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UserLogin(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string pwd = txtPassword.Text.Trim();
            string code = txtCode.Text.Trim();

            string imgCode = Session["imgCode"] as string;

            if (imgCode != code)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "error", "<script>$(function(){showErrMsg('验证码错误！');});</script>");
                return;
            }

            Users user = Users.GetUserByUserName(userName);

            if (user == null)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "error", "<script>$(function(){showErrMsg('用户名不存在！');});</script>");
                return;
            }

            pwd = BabyMd5.Md5Encrypt(pwd, Encoding.UTF8).ToUpper();

            if (user.U_Password != pwd)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "error", "<script>$(function(){showErrMsg('密码错误！');});</script>");
                return;
            }

            Request.Cookies.Add(new System.Web.HttpCookie("Baby_GrowUp_UserName", user.U_UserName));
            Request.Cookies.Add(new System.Web.HttpCookie("Baby_GrowUp_UserId", user.U_Id));

            Response.Redirect("~/");
        }
    }
}